// 这个文件专门存放关于“我的”模块的所有接口

const customer=require('koa-router')()
const db=require('../util/db')


// get请求
// https://api.lzjlw.top:3030/customer/collections?userId=001
customer.get('/collections',async (ctx)=>{
    let userId=ctx.query.userId
    console.log(userId)
    let selectSql=`select collections.videoId,video_Img,videoUrl,video_Title,play_Time,type,stage from interactive_video,collections where userId=${userId} and collections.videoId=interactive_video.videoId`
    let res=await new Promise((resolve,reject)=>{
        db.query(selectSql,(err,result)=>{
            if(err){
                throw err
            }else{
                resolve(result)
            }
        })
    })
    ctx.body=res
})

// https://api.lzjlw.top:3030/customer/thumbs_Up?userId=001 && type=1
customer.get('/thumbs_Up',async (ctx)=>{
    let obj=ctx.query
    // 利用正则表达式去除空格
    str = JSON.stringify(obj).toString().replace(/\s*/g,"")
    newObj=JSON.parse(str)
    let selectSql=`select videoId,video_Img,videoUrl,video_Title,play_Time,thumbs_up.type,stage from interactive_video,thumbs_up 
    where userId=${newObj.userId} and thumbs_up.type=${newObj.type} and thumbs_up.from_id=interactive_video.videoId`
    let res=await new Promise((resolve,reject)=>{
        db.query(selectSql,(err,result)=>{
            if(err){
                throw err
            }else{
                resolve(result)
            }
        })
    })
    ctx.body=res
})

// https://api.lzjlw.top:3030/customer/play_History?userId=001
customer.get('/play_History',async (ctx)=>{
    let obj=ctx.query
    // 利用正则表达式去除空格
    str = JSON.stringify(obj).toString().replace(/\s*/g,"")
    newObj=JSON.parse(str)
    let selectSql=`select play_history.videoId,video_Img,videoUrl,video_Title,play_history.play_Time,type,stage from interactive_video,play_history 
    where userId=${newObj.userId} and play_history.videoId=interactive_video.videoId`
    let res=await new Promise((resolve,reject)=>{
        db.query(selectSql,(err,result)=>{
            if(err){
                throw err
            }else{
                resolve(result)
            }
        })
    })
    ctx.body=res
})



// post请求
// https://api.lzjlw.top:3030/customer/user
customer.post('/user',async (ctx)=>{
    let obj=ctx.request.body
    console.log(obj)
    // 生成唯一userId(bigint)
    let userId=Math.floor((Math.random()*1000000))
    let insertSql=`insert into user(userId,userName,sex,age,head_Photo) values(${userId},'${obj.userName}','${obj.sex}',${obj.age},'${obj.head_Photo}')`
    console.log(insertSql)
    let user=[userId,`${obj.userName}`,`${obj.sex}`,obj.age,`${obj.head_Photo}`]
    let res=await new Promise((resolve,reject)=>{
        db.query(insertSql,(err,result)=>{
            if(err){
                throw err
            }
            else{
                resolve(user)
            }
        })
    })
    ctx.body=res
})


// 修改信息
// https://api.lzjlw.top:3030/customer/user/1
customer.post('/user/:userId',async (ctx)=>{
    let userId=ctx.params.userId
    let obj=ctx.request.body
    console.log(userId)
    let updateSql=`update user set userName='${obj.userName}',age=${obj.age},sex='${obj.sex}' where userId=${userId} `
    let res=await new Promise((resolve,reject)=>{
        db.query(updateSql,(err,result)=>{
            if(err){
                throw err
            }
            else{
                resolve('update success')
            }
        })
    })
    ctx.body=res
})



// delete请求
// https://api.lzjlw.top:3030/customer/collections?userId=1 && videoId=1
customer.delete('/collections',async (ctx)=>{
    let obj=ctx.query
    // 利用正则表达式去除空格
    str = JSON.stringify(obj).toString().replace(/\s*/g,"")
    newObj=JSON.parse(str)
    let deleteSql=`delete from collections where userId=${newObj.userId} and videoId=${newObj.videoId}`
    let res=await new Promise((resolve,reject)=>{
        db.query(deleteSql,(err,result)=>{
            if(err){
                throw err
            }else{
                resolve('delete success')
            }
        })
    })
    ctx.body=res
})


// https://api.lzjlw.top:3030/customer/thumbs_Up?userId=1 && videoId=1 && type=1
customer.delete('/thumbs_Up',async (ctx)=>{
    let obj=ctx.query
    // 利用正则表达式去除空格
    str = JSON.stringify(obj).toString().replace(/\s*/g,"")
    newObj=JSON.parse(str)
    let deleteSql=`delete from thumbs_up 
    where userId=${newObj.userId} and from_Id=${newObj.videoId} and type=${newObj.type}`
    let res=await new Promise((resolve,reject)=>{
        db.query(deleteSql,(err,result)=>{
            if(err){
                throw err
            }else{
                resolve('delete success')
            }
        })
    })
    ctx.body=res
})

// https://api.lzjlw.top:3030/customer/play_History?userId=1 && videoId=1
customer.delete('/play_History',async (ctx)=>{
    let obj=ctx.query
    // 利用正则表达式去除空格
    str = JSON.stringify(obj).toString().replace(/\s*/g,"")
    newObj=JSON.parse(str)
    let deleteSql=`delete from play_history
    where userId=${newObj.userId} and videoId=${newObj.videoId}`
    let res=await new Promise((resolve,reject)=>{
        db.query(deleteSql,(err,result)=>{
            if(err){
                throw err
            }else{
                resolve('delete success')
            }
        })
    })
    ctx.body=res
})

module.exports=customer